package com.qf.aop;

import com.qf.pojo.AccountLog;
import com.qf.service.AccountLogService;
import com.utils.ThreadLocalUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;

@Component
@Aspect
public class MyAspect {

    @Autowired
    private AccountLogService accountLogService;


    @Pointcut("execution(* com.qf.service.impl..*.delete*(..))")
    public void pt(){}

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("切面执行...");

        Object proceed = pjp.proceed();

        //记录删除日志
        String methodName = pjp.getSignature().getName();
        Integer uid = ThreadLocalUtils.getUid();
        String params = Arrays.toString(pjp.getArgs());

        AccountLog accountLog = new AccountLog();
        accountLog.setOperation(methodName);
        accountLog.setOperationTime(new Date());
        accountLog.setOperationId(uid);
        accountLog.setOperationParams(params);

        accountLogService.save(accountLog);

        return proceed;
    }
}
